﻿using System;
using System.Collections.Generic;

namespace Bibylon.BehafX.StateMachine.concept
{
    
    internal abstract class composite : node
    {
        /// <summary>
        /// 'composite-category' is of multi-child-nodes 
        /// </summary>
        public composite(string name, int nid, node parent)
            : base(name, nid, parent)
        { 
            Children = new List<node>();
        }


        /// <summary>
        /// @Copydoc - node::Category
        /// 
        /// @Remark - this type of block contains one or more childeren 
        /// and execute them in specific order. (none leaf node)
        /// </summary>
        // public override string Category { get { return "Composite"; } }


        /// <summary>
        /// Index of the child which is running
        /// </summary>
        public int OngoingChild { get; protected set; }

        /**
        /// <summary>
        /// Get child count, you can get all the descendants of this node
        /// </summary>
        public int getNumberOfChildren(bool directOnly = true)
        {
            int count = Children.Count;

            // if we want childrens' children count 
            if (directOnly == false)
            {
                foreach (node child in Children)
                {   
                    count += child.getNumberOfChildren(false);
                }
            }

            return count;
        }
        */


        /// <summary>
        /// Append a child node to list of Children 
        /// </summary>
        public virtual void addChild(node child, object userAny)
        {
            if (child != null)
            {
                child.Parent = this;
                Children.Add(child);
            }
        }

        /// <summary>
        /// Remove the given child from this
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public virtual bool dropChild(node child)
        {
            foreach (node c in Children)
            {
                if (c == child)
                {
                    Children.Remove(c);
                    return true; //break;
                }
            }

            return false;
        }

        /// <summary>
        /// @Copydoc - node::shutdown
        /// </summary>
        internal override void shutdown(object agent, bool applyToChildren = false)
        {
            // OngoingChild = -1;
            base.shutdown(agent, applyToChildren);

            if ( applyToChildren )
            {
                foreach (node child in Children)
                {
                    if (child != null)
                        child.shutdown(agent, applyToChildren);
                }
            }
        }

    }
}
